package com.bkitmobile.kesu.android.GUI;

import org.json.JSONObject;

import com.bkitmobile.kesu.android.connectServer.Request;
import com.bkitmobile.kesu.android.connectServer.ServiceCallback;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.text.Html;
import android.text.Spanned;

public final class StaticDialogs {
	public static void okButtonDialog(Context context, String mes) {
		if(mes == null)
			mes = "messenger is null";
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
    	builder.setMessage(mes)
    	       .setNegativeButton("OK", new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	                dialog.cancel();
    	           }
    	       });

    	AlertDialog alert = builder.create();
    	alert.setCanceledOnTouchOutside(true);

    	alert.show();
    	
	}
	public static void okButtonDialog(Context context, Spanned mes) {
		if(mes == null)
			mes = Html.fromHtml("messenger is null");
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
    	builder.setMessage(mes)
    	       .setNegativeButton("OK", new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	                dialog.cancel();
    	           }
    	       });

    	AlertDialog alert = builder.create();
    	alert.setCanceledOnTouchOutside(true);

    	alert.show();
    	
	}
	
	public static void okButtonDialog(Context context, String title, String mes) {
		if(mes == null)
			mes = "messenger is null";
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
    	builder.setMessage(mes)
    	       .setTitle(title)
    	       .setNegativeButton("OK", new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	                dialog.cancel();
    	           }
    	       });

    	AlertDialog alert = builder.create();
    	alert.setCanceledOnTouchOutside(true);

    	alert.show();
    	
	}
	public static void okButtonDialog(Context context, String title, Spanned mes) {
		if(mes == null)
			mes = Html.fromHtml("messenger is null");
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
    	builder.setMessage(mes)
    		   .setTitle(title)
    	       .setNegativeButton("OK", new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	                dialog.cancel();
    	           }
    	       });

    	AlertDialog alert = builder.create();
    	alert.setCanceledOnTouchOutside(true);

    	alert.show();
    	
	}
	
	public static void profileDialog(final Context context, final String id){
		String []identities = {"ID", "friend"};
		String []atributies = {Request.GET_FRIEND_PROFILE, id};
		final Handler handler = new Handler();
		Request.sendAndRecieveData(identities, atributies, true,context,new ServiceCallback<String>() {
			
			@Override
			public void onSuccess(final String result) {
				// TODO Auto-generated method stub
				handler.post(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						System.out.println("Profile " + result);
						try{
							JSONObject obj = new JSONObject(result);
							if(obj.getString("result").startsWith("succ")){
								try {
								Spanned span = Html.fromHtml("<b>" +  context.getString(R.string.setting_fname) + " </b>"
															+ solve(obj.getString("fName")) + "<br/>" 
															+ "<b>" +  context.getString(R.string.setting_lname) + " </b>"
															+ solve(obj.getString("lName")) + "<br/>" 
															+ "<b>" +  context.getString(R.string.setting_sex) + " </b>"
															+ solve(obj.getString("sex")) + "<br/>" 
															+ "<b>" +  context.getString(R.string.setting_bdate) + " </b>"
															+ solve(obj.getString("bDay")) + "<br/>" 
															+ "<b>" +  context.getString(R.string.setting_email) + " </b>"
															+ solve(obj.getString("email")) + "<br/>" 
															);
								okButtonDialog(context, id, span);
								} catch (Exception e)
								{
									
								}
							}
							else{
								
								okButtonDialog(context, result);
							}
						} catch (Exception e)
						{
							
						}
					}
				});
			}
			
			@Override
			public void onFailure(Exception exception) {
				// TODO Auto-generated method S
				handler.post(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						StaticDialogs.okButtonDialog(context
								, context.getString(R.string.disconnect));
					}
				});
				
			}
		});
		
		
	}
	private static String solve(String str){
		if(str.equals("")){
			return "No Infomation";
		}
		else
			return str;
	}
}
